
app.controller('newMenuCtrl', function($element, $scope, $state, rest, user, uploadConf, tipService, $window, $rootScope) {
	//$scope.myUpMenus = angular.copy($scope.ngDialogData, {});
	//$scope.menuLookup = $scope.myUpMenus.datas[0];  //设置默认所属菜单为第一个 请选择
	
	//新建$scope.iNowData全默认为空
	$scope.iNowData = {
		"menuName":"",
		"sort":"",
		"menuPath":"",
		"remark":""
	};
    
    $scope.init = function(){
    	//菜单类型
    	$scope.menuTypeList = [
    		{
    			"menuType" : "INTERNAL",
    			"name" : "内部"
    		},
    		{
    			"menuType" : "EXTERNAL",
    			"name" : "外部"
    		}
    	];
    	$scope.menuTypeVal = $scope.menuTypeList[0];
    };
    $scope.init();
    
    var token = $window.localStorage.getItem("token");
	rest.decoJava({
	   	 type: "POST",
	     url: "/tenancy-sys/admin/menu/all/list",
	     data: '',
	     //contentType:"application/json",
	     beforeSend: function(request){
			request.setRequestHeader("token", token); 
		},
	    success: function(data){ 
	    	
	    	$scope.upMenuDatas = [{menuName: "请选择", id: ""}];
            angular.forEach(data.result,function(item){
            	if(item.level==0){
            		$scope.upMenuDatas.push(item);
            	}
            })
	    	
		    $scope.menuLookup = $scope.upMenuDatas[0];
		    $scope.$apply()
		    
           angular.element("#txt_boostrap_icon").iconPicker();  //启用图标选择差距安
	    }
    });
    
    //保存
    $scope.preserveSubmit = function(){
    	var menuIconCss = angular.element("#txt_boostrap_icon").val();  //图标选择插件必须要用jq去获取value，ng-model是绑定不到的
    	//判断新建按钮所属菜单有没有选 避免报错
    	if($scope.menuLookup == undefined){
    		var menuLookupId = "";
    	} else{
    		var menuLookupId = $scope.menuLookup.id;
    	}
    	var formData = {
    		"menuName" : $scope.iNowData.menuName,  //菜单名称
    		"menuIconCss" : menuIconCss,  //图标
    		"sort" : $scope.iNowData.sort,  //排序字符
    		"upId" : menuLookupId,  //所属菜单id
    		"menuPath" : $scope.iNowData.menuPath,  //请求路径
    		"menuType" : $scope.menuTypeVal.menuType,  //菜单类型
    		"remark" : $scope.iNowData.remark  //备注
    	}
    	var btn = true;  //提交开关
    	//非空校验
    	if($scope.iNowData.menuName == ""){
    		layer.tips('菜单名称必填', '.menuName-Tips',{
			  tipsMore: true,
			  tips: [2,'#ed5565']
			});
    		btn =  false;
    	};
    	if($scope.iNowData.sort == ""){
    		layer.tips('排序字符必填', '.sort-Tips',{
			  tipsMore: true,
			  tips: [2,'#ed5565']
			});
    		btn =  false;
    	};
    	if($scope.iNowData.menuPath == ""){
    		layer.tips('请求路径必填1', '.menuPath-Tips',{
			  tipsMore: true,
			  tips: [2,'#ed5565']
			});
    		btn =  false;
    	};
    	if($scope.iNowData.menuType == ""){
    		layer.tips('菜单类型必选', '.menuType-Tips',{
			  tipsMore: true,
			  tips: [2,'#ed5565']
			});
    		btn =  false;
    	};
    	//console.log($scope.iNowData.menuName == "",$scope.iNowData.tagName == "",$scope.iNowData.sort == "",$scope.iNowData.menuPath == "",$scope.iNowData.menuType == "")
    	if(btn){
    		console.log(formData)
    		var token = $window.localStorage.getItem("token");
    		rest.decoJava({
    			type: 'post',
	            url: '/tenancy-sys/admin/menu/add',///settlement-center-service/admin/menu/save.do
	            data: JSON.stringify(formData),
	            contentType:"application/json; charset=utf-8",
	            beforeSend: function(request){
					request.setRequestHeader("token", token); 
				},
	            success: function (result) {
	            	console.log(result);
	            	layer.closeAll('loading');
	                if (result) {
	                    if ("0000" == result.code) {
							layer.msg(result.message, {
					        	offset:'t',
					        	icon: 1,
								time: 0, //不自动关闭
								btn: ['确定'],
								yes: function(index){
									layer.close(index);
									$scope.closeThisDialog();
							    }
							});
	                    } else {
	                    	layer.msg(result.message,{
			    				offset:'t'
			    			});
	                    }
	                }
	            }
	        });
    	}
    };
    
    $scope.hideBtn = function(){
    	$scope.closeThisDialog();
    }
    
});
